//LOGIN verification
function checkFormLogin(form) {
    var userName = form.user;
    var pass = form.pass;
    var confPass = form.pass;
    return check(userName, pass, confPass);

}

function checkFormUser(form) {
    var userName = form.userName;
    var pass = form.passw;
    var confPass = form.confPassword;
    return check(userName, pass, confPass);
}

function check(userName, pass, confPass) {
    if (userName.value == "") {
        alert("Userame cannot be blank!");
        userName.focus();
        return false;
    }
    else {
        var re = /^\w+$/;
        if (!re.test(userName.value)) {
            alert("Userame must contain only letters, numbers and underscores!");
            userName.focus();
            return false;
        }
    }

    if (pass.value != "") {
        if (pass.value.length < 6) {
            alert("Passord must contain at least six characters!");
            pass.focus();
            return false;
        }
        else {
            re = /[0-9]/;
            if (!re.test(pass.value)) {
                alert("Passord must contain at least one number (0-9)!");
                pass.focus();
                return false;
            }
        }
    }
    else {
        alert("Passord cannot be blank!");
        pass.focus();
        return false;
    }
    if (pass.value == userName.value) {
        alert("Passord must be different from Userame!");
        pass.focus();
        return false;
    }

    //user verification only
    if (confPass.value == "") {
        alert("Please confirm your password!");
        return false
    }
    else {
        if (pass.value != confPass.value) {
            alert("The passwords you entered do not match. Please try again!")
            return false;
        }
    }
    return true;
}

//SelectedItem
function submitForm(form) {
    document.forms[form].submit();
}
function validateAndSubmit(form) {
    var select = document.forms[form].select.selectedIndex;
    if (select == -1) {
        alert("Please select an item from the list");
        document.getElementById('hiddenField').value = "";
        return false;
    }
    else {
        submitForm(form);
        return true;
    }
}
//users validations
function operEdit(opEdit, form) {
    document.getElementById('hiddenField').value = opEdit;
    submitForm(form);
}
function operDelete(opDelete, form) {
    document.getElementById('hiddenField').value = opDelete;
    submitForm(form);
}

function operDeleteUser(opDelete, form, currentUser){
    var select = $("#"+form+" select option:selected").get(0).text;
    if (select != currentUser){
        operDelete(opDelete, form);
    }else{
        alert("You can not delete yourself");
    }
}

function operAdd(opAdd, form) {
    document.getElementById('hiddenField').value = opAdd;
    submitForm(form);
}

//test validations

function checkFormTest(form) {
    var test = form.name;
    return checkTA(test);
}

function checkTA(name) {
    if (name.value == "") {
        alert("Please insert a test name");
        return false
    }
    return true;
}

//questions and question validation
function activateButton(editButtonId, formName, deleteButtonId) {
    var select = document.forms[formName].select.selectedIndex;
    if (select != -1) {
        $("#" + editButtonId).removeAttr("disabled");
        $("#" + deleteButtonId).removeAttr("disabled");

    }
}

function enableButton(button, textArea, hintInput) {

    var textAreaValue = $('#' + textArea).val();
    var hintVal = $('#' + hintInput).val();
    var ind = $('#newQuestionFormId input[name^=answ]').length;
    if (textAreaValue != null && textAreaValue != "" && hintVal != null && hintVal != "" && ind > 2) {
        $("#" + button).removeAttr("disabled");
    } else {
        $("#" + button).attr("disabled", "disabled");
        return;
    }
    var ok = true;


    $('#newQuestionFormId input[name^=answ]').each(function () {
        var txt = $(this).val();
        var name = $(this).attr('name');
        if ((txt == null || txt == "") && (name != "answ" + ind)) {
            ok = false;
        }
    });
    if (ok && ind > 2) {
        $("#" + button).removeAttr("disabled");
    } else {
        $("#" + button).attr("disabled", "disabled");
        return;
    }
}

function validateAddQuestion(form, url) {
    var ok = true;
    $('#newQuestionFormId input[name^=answ]').each(function () {
        var txt = $(this).val();
        var name = $(this).attr('name');

        if ((txt == null || txt == "")) {
            ok = false;
        }
    });
    if (ok == false) {
        alert("Please fill all the answers");
        document.getElementById('hiddenField').value = "";
        return false;
    } else {
        document.getElementById('hiddenField').value = url;
        submitForm("newQuestionFormId");
        return true;

    }
}

//test validations
function enableButtonAddTest(button, textArea) {
    var textAreaValue = $('#' + textArea).val();
    if (textAreaValue != null && textAreaValue != "") {
        $("#" + button).removeAttr("disabled");
    } else {
        $("#" + button).attr("disabled", "disabled");
        return;
    }
}


//userPart validation
